﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using PMIS.BLL;
using PMIS.BLL.Post;
using PMIS.BLL.User;

namespace PMIS.UI.User
{
	/// <summary>
	/// 人员列表窗体
	/// </summary>
	public partial class 人员列表 : BaseDockForm
	{
		#region 私有变量
		/// <summary>
		/// 当前的查询参数
		/// </summary>
		private UserQueryParamer CurrentQueryParamer = null;
		#endregion

		#region 单例
		/// <summary>
		/// 本类的实例
		/// </summary>
		private static 人员列表 _form = null;
		/// <summary>
		/// 用于本类实现单例的Locker
		/// </summary>
		private static object locker = new object();
		/// <summary>
		/// 构造函数
		/// </summary>
		private 人员列表()
		{
			InitializeComponent();
		}
		/// <summary>
		/// 获取本窗体的实例
		/// </summary>
		/// <returns></returns>
		public static 人员列表 GetInstance()
		{
			if (_form == null || _form.IsDisposed)
			{
				lock (locker)
				{
					if (_form == null || _form.IsDisposed)
						_form = new 人员列表();
				}
			}

			return _form;
		}
		#endregion

		#region 窗体事件
		/// <summary>
		/// 窗体载入事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void UserListForm_Load(object sender, EventArgs e)
		{
			try
			{
				//	清空查询条件
				tsbtReset_Click(null, null);
				//	默认查询
				tsbtQuery_Click(null, null);
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		#endregion

		#region 界面显示
		/// <summary>
		/// 更新岗位列表
		/// </summary>
		private void UpdatePostList()
		{
			try
			{
				//	获取所有的岗位
				List<岗位> list = Singleton<BL_岗位>.GetInstance().GetAll();
				//	绑定岗位列表
				cbPost.BeginUpdate();
				cbPost.Items.Clear();
				cbPost.Items.Add("不限");
				foreach (岗位 item in list)
					cbPost.Items.Add(item);
				cbPost.EndUpdate();
				cbPost.SelectedIndex = 0;
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 更新列表显示
		/// </summary>
		private void UpdateListView()
		{
			try
			{
				if (this.InvokeRequired)
				{
					this.Invoke(new Action(UpdateListView));
				}
				else
				{
					toolStrip1.Enabled = dgvList.Enabled = wp.Enabled = false;

					//	分页参数
					PagingParameter pp = new PagingParameter();
					pp.CurrentPageIndex = wp.CurrentPageIndex;
					pp.PageSize = wp.PageSize;
					//pp.OrderBy = "姓名 Asc";

					//	查询并显示
					dgvList.AutoGenerateColumns = false;
					dgvList.DataSource = Singleton<BL_人员>.GetInstance().Query(CurrentQueryParamer, ref pp);

					//	记录总数
					wp.TotalRecordCount = pp.TotalRecordCount;
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
			finally
			{
				toolStrip1.Enabled = dgvList.Enabled = wp.Enabled = true;
			}
		}
		#endregion

		#region 触发事件
		/// <summary>
		/// 翻页事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void wp_PageIndexChanged(object sender, EventArgs e)
		{
			tsbtRefresh_Click(null, null);
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtQuery_Click(object sender, EventArgs e)
		{
			try
			{
				//	查询参数
				CurrentQueryParamer = new UserQueryParamer();
				//	工号
				CurrentQueryParamer.工号 = tbNo.Text.Trim();
				//	姓名
				CurrentQueryParamer.姓名 = tbName.Text.Trim();
				//	性别
				CurrentQueryParamer.性别 = cbSex.SelectedIndex == 0 ? string.Empty : cbSex.Text;
				//	岗位代码
				CurrentQueryParamer.岗位代码 = tbJobCode.Text.Trim();
				//	身份证号码
				CurrentQueryParamer.身份证号码 = tbIDNumber.Text.Trim();
				//	部门
				CurrentQueryParamer.部门班组 = tbDepart.Text.Trim();
				//	岗位
				CurrentQueryParamer.岗位 = tbJob.Text.Trim();
				//	备注
				CurrentQueryParamer.备注 = tbRemark.Text.Trim();
				//	出生时间_开始
				CurrentQueryParamer.出生时间_开始 = dtpBirthStart.Checked ? (DateTime?)dtpBirthStart.Value : null;
				//	出生时间_结束
				CurrentQueryParamer.出生时间_结束 = dtpBirthEnd.Checked ? (DateTime?)dtpBirthEnd.Value : null;
				//	工作时间_开始
				CurrentQueryParamer.工作时间_开始 = dtpWorkStart.Checked ? (DateTime?)dtpWorkStart.Value : null;
				//	工作时间_结束
				CurrentQueryParamer.工作时间_结束 = dtpWorkEnd.Checked ? (DateTime?)dtpWorkEnd.Value : null;
				//	岗位
				CurrentQueryParamer.岗位ID = cbPost.SelectedIndex < 1 ? string.Empty : (cbPost.SelectedItem as 岗位).ID;

				//	更新列表
				tsbtRefresh_Click(null, null);
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 清空
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtReset_Click(object sender, EventArgs e)
		{
			tbNo.Text = string.Empty;
			tbName.Text = string.Empty;
			cbSex.SelectedIndex = 0;
			tbJobCode.Text = string.Empty;
			tbIDNumber.Text = string.Empty;
			tbDepart.Text = string.Empty;
			tbJob.Text = string.Empty;
			tbRemark.Text = string.Empty;
			dtpBirthStart.Value = dtpBirthEnd.Value = dtpWorkStart.Value = dtpWorkEnd.Value = DateTime.Now;
			dtpBirthStart.Checked = dtpBirthEnd.Checked = dtpWorkStart.Checked = dtpWorkEnd.Checked = false;
			cbPost.SelectedIndex = cbPost.Items.Count > 0 ? 0 : -1;
			//	更新岗位列表
			UpdatePostList();
		}
		/// <summary>
		/// 新增
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtAdd_Click(object sender, EventArgs e)
		{
			try
			{
				人员信息维护 form = 人员信息维护.GetInstance("new");
				if (form == null)
					throw new Exception("获取人员信息维护窗体时发生错误!");

				if (form.ShowDialog(this) == DialogResult.OK)
				{
					tsbtRefresh_Click(null, null);
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 修改
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtModify_Click(object sender, EventArgs e)
		{
			if (dgvList.SelectedRows.Count == 0)
			{
				ShowErrorMessage("请选择要修改的记录!");
				return;
			}

			try
			{
				string ID = dgvList.SelectedRows[0].Cells[ColumnID.Index].Value as string;
				if (string.IsNullOrEmpty(ID))
					return;

				人员信息维护 form = 人员信息维护.GetInstance(ID);
				if (form == null)
					throw new Exception("获取人员信息维护窗体时发生错误!");

				if (form.ShowDialog(this) == DialogResult.OK)
				{
					tsbtRefresh_Click(null, null);
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtDelete_Click(object sender, EventArgs e)
		{
			if (dgvList.SelectedRows.Count == 0)
			{
				ShowErrorMessage("请选择要删除的记录!");
				return;
			}

			if (ShowYesNoMessage("确定删除该记录吗?") != DialogResult.Yes)
				return;

			try
			{
				string ID = dgvList.SelectedRows[0].Cells[ColumnID.Index].Value as string;
				if (string.IsNullOrEmpty(ID))
					return;

				if (Singleton<BL_人员>.GetInstance().Delete(ID))
				{
					ShowSuccessMessage("记录删除成功!");
					//	更新列表
					tsbtRefresh_Click(null, null);
				}
				else
				{
					ShowErrorMessage("删除记录时发生错误!");
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 刷新
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtRefresh_Click(object sender, EventArgs e)
		{
			new Thread(new ThreadStart(UpdateListView)).Start();
			//new Action(UpdateListView).BeginInvoke(null, null);
		}
		/// <summary>
		/// 退出
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}
		/// <summary>
		/// 列表双击事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void dgvList_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.RowIndex < 0)
				return;

			try
			{
				string ID = dgvList.Rows[e.RowIndex].Cells[ColumnID.Index].Value as string;
				if (string.IsNullOrEmpty(ID))
					return;

				人员信息维护 form = 人员信息维护.GetInstance(ID);
				if (form == null)
					throw new Exception("无法创建人员维护信息窗体");

				if (form.ShowDialog(this) == DialogResult.OK)
					tsbtRefresh_Click(null, null);
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		#endregion
	}
}
